package com.bbdjt.weixin.web.action.forumPost;


import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bbdjt.weixin.base.action.BaseAction;
import com.bbdjt.weixin.model.ForumPost;
import com.bbdjt.weixin.model.ForumPostQueryModel;
import com.bbdjt.weixin.model.LeaveWord;
import com.bbdjt.weixin.service.ForumPostService;
import com.bbdjt.weixin.service.LeaveWordService;
import com.github.pagehelper.PageInfo;

/**
 * @项目名称: bbdjt
 * @类名称: ForumPostAction
 * @类描述: ForumPost表相关操作
 * @创建人: guoLiang
 * @创建时间: 2017-03-20 12:05:23
 * @修改人: 
 * @修改时间: 
 * @修改备注:
 * @version
 */
@Controller
public class ForumPostAction extends BaseAction {
	
	@Autowired
	private ForumPostService forumPostService;
	
	@Autowired
	private LeaveWordService leaveWordService;

	private String page_list = "jsp/forumPost/forumPostindex";

	private String redirect_list = "redirect:r_forumPost";
	
	private static Logger logger = Logger.getLogger(ForumPostAction.class);
	
	
	// 分页查询
	@RequestMapping(value = "r_forumPost")
	public ModelAndView getListPage(ForumPostQueryModel forumPost) {
		ModelAndView result = new ModelAndView(page_list);
		forumPost.setOrderBy("id asc");
		List<ForumPost> forumPostList = forumPostService.selectByForumPost(forumPost);
		result.addObject("pageInfo", new PageInfo<ForumPost>(forumPostList));
		result.addObject("forumPost", forumPost);
		return result;
	}

	// 自定义分页查询 根据xml 分页查询
	@RequestMapping(value = "r_forumPostXml")
	public ModelAndView getListPageXml(ForumPostQueryModel forumPost) {
		ModelAndView result = new ModelAndView(page_list);
		forumPost.setOrderBy("id asc");
		PageInfo<ForumPostQueryModel> pageInfo = forumPostService.selectByForumPostQueryModel(forumPost);
		List<ForumPostQueryModel> forumPostList = pageInfo.getList();
		result.addObject("pageInfo", new PageInfo<ForumPostQueryModel>(forumPostList));
		result.addObject("forumPost", forumPost);
		return result;
	}

	// 返回整个页面的 json 包括分页 也签 内容 标示 和 正文 内容 同上方法 只不过是返回json
	@RequestMapping(value = "r_forumPostJson", produces = "application/json")
	public ModelAndView pageInfo(ForumPostQueryModel forumPost) {
		ModelAndView result = new ModelAndView();
		PageInfo<ForumPostQueryModel> pageInfo = forumPostService.selectByForumPostQueryModel(forumPost);
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	//ligerui table
	@ResponseBody
	@RequestMapping(value = "r_forumPostLigerUi", produces="application/json;charset=UTF-8") 
	public String pageInfoLigerUi(ForumPostQueryModel forumPost) {
		forumPost.setOrderBy(forumPost.getSortname(),forumPost.getSortorder());
		PageInfo<ForumPostQueryModel> pageInfo = forumPostService.selectByForumPostQueryModel(forumPost);
		return super.toLigerUiSelectJson(pageInfo);
	}

	// 添加数据 添加或者修改数据页面 主键为id
	// 如果没有id 不查 如果有查询补上信息
	@RequestMapping(value = "v_forumPost")
	public ModelAndView add(ForumPost forumPost) {
		ModelAndView result = new ModelAndView();
		if (forumPost.getId() != null) {
			forumPost = forumPostService.selectByKey(forumPost.getId());
		}
		result.addObject("forumPost", forumPost);
		return result;
	}

	// 如果有id就更新 没有 就插入
	@RequestMapping(value = "c_forumPost")
	public ModelAndView save(ForumPost forumPost) {
		ModelAndView result = new ModelAndView(redirect_list);
		if (forumPost.getId() != null) {
			forumPostService.updateNotNull(forumPost);
		} else {
			// 会保存null 值
			forumPostService.insertSelective(forumPost);
		}
		return result;
	}

	// 删除数据
	@RequestMapping("d_forumPost")
	public String delete(Integer id) {
		forumPostService.delete(id);
		return redirect_list;
	}
	
	//查询所有论坛帖子
	@ResponseBody
	@RequestMapping(value = "selectAllForumPost")
	public ModelAndView selectAllForumPost (ForumPost forumPost){
		ModelAndView result = new ModelAndView("bbs/bbs");
		List<ForumPost> forumPostList = forumPostService.selectAllForumPost(forumPost);
		result.addObject("forumPostList", forumPostList);
		return result;
	}
	
	//查询帖子详情
	@ResponseBody
	@RequestMapping(value = "selectDetailsForumPost")
	public ModelAndView selectDetailsForumPost (ForumPost forumPost){
		ModelAndView result = new ModelAndView("bbs/bbscont");
		LeaveWord leaveWord = new LeaveWord();
		if(forumPost.getId() != null){
			leaveWord.setForumPostId(forumPost.getId());
			forumPost = forumPostService.selectDetailsForumPost(forumPost);
			List<LeaveWord> leaveWordList = leaveWordService.selectAllleaveWord(leaveWord);
			result.addObject("forumPost", forumPost);
			result.addObject("leaveWordList", leaveWordList);
		}
		return result;
	}
	
	//患者发布帖子
	@ResponseBody
	@RequestMapping(value = "addForumPost")
	public String addForumPost (ForumPost forumPost){
		if(forumPost.getPatientId() != null && forumPost.getForumContent() != null){
			forumPost.setCreateDate(new Date());
			forumPostService.insertSelective(forumPost);
			return super.writeOK(forumPost);
		}else{
			logger.info("患者ID为空:" + forumPost.getPatientId());
			return super.lackError();
		}
	}
}